package com.game.data;

import com.game.config.Coefficient;
import com.game.model.*;
import com.game.model.api.*;
import com.game.model.equip.Equip;
import com.game.model.goods.Goods;
import com.game.model.role.Monster;
import com.game.model.role.NPC;
import com.game.model.role.PlayerRole;
import com.game.model.role.Role;
import com.game.model.task.Task;
import com.game.utils.Format;
import com.game.utils.JDBCUtil;

import java.io.Serializable;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:角色生成数据
 **/
public class RoleData implements Serializable {
    public static Role playerRole;//我
    public static Map<EquipType, Equip> equipMap;//我的装备
    public static Map<Integer, Task> taskMap;//我的任务
    public static Map<Integer, NPC> npcMap;//npc
    public static List<Role>roleList;//游戏所有角色
    static {
        playerRole = getRoleById(1);
        equipMap = getEquipMap();
        taskMap = getTaskMap();
        roleList = roleInit();
        npcMap = npcInit();
    }

    //装备槽初始化
    public static Map<EquipType, Equip> getEquipMap() {
        HashMap<EquipType, Equip> equipMap = new HashMap<>();
        equipMap.put(EquipType.WEAPON, null);
        equipMap.put(EquipType.HEAD, null);
        equipMap.put(EquipType.BODY, null);
        equipMap.put(EquipType.LEG, null);
        equipMap.put(EquipType.SHOES, null);
        return equipMap;
    }

    //我的任务初始化
    public static Map<Integer, Task> getTaskMap() {
        HashMap<Integer, Task> taskMap = new HashMap<>();
        return taskMap;
    }

    public static Role getRoleById(int id) {
        if (id==1){
            return new PlayerRole(1, 1, RoleType.WARRIOR, 100, getBaseProperty(), getSpecialProperty());
        }
        for (Role r : roleList) {
            if (r.getId() == id) {
                return r;
            }
        }
        return null;
    }



    public static BaseProperty getBaseProperty() {
        return new BaseProperty(100, 100, 200, 100);
    }

    public static SpecialProperty getSpecialProperty() {
        return new SpecialProperty(10, 50, 0, 10);
    }


    public static List<Role> getMonsters() {
        ArrayList<Role> roles = new ArrayList<>();
        if ("system".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
        } else if ("mysql".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            try {
                ResultSet rs = JDBCUtil.executeQuery("select * from monster");
                while (rs.next()) {
                    Monster monster = new Monster();
                    monster.setId(rs.getInt("id"));
                    monster.setLevel(rs.getInt("level"));
                    monster.setBaseProperty((BaseProperty) PropertyData.getBasePropertyById(rs.getInt("base_property")));
                    monster.setSpecialProperty((SpecialProperty) PropertyData.getSpecialPropertyById(rs.getInt("special_property")));
                    monster.setMonsterType(Format.getMonsterType(rs.getInt("monster_type")));
                    monster.setDescription(rs.getString("description"));
                    monster.setName(rs.getString("name"));
                    if (rs.getString("rewards") == null || "".equals(rs.getString("rewards"))) {
                        monster.setRewards(null);
                    } else {
                        //获取奖励字符串
                        String[] rewards = rs.getString("rewards").split("-");
                        HashMap<Goods, Integer> goodsMap = new HashMap<>();
                        for (String reward : rewards) {
                            //分割id与数量
                            int goodsId = Integer.parseInt(reward.split(",")[0]);
                            int count = Integer.parseInt(reward.split(",")[1]);
                            goodsMap.put(GoodsData.getGoods(goodsId), count);
                        }
                        monster.setRewards(goodsMap);
                    }
                    roles.add(monster);
                }
                JDBCUtil.close(rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return roles;
    }

    //NPC初始化
    public static List<Role> getNpcs() {
        ArrayList<Role> npcs = new ArrayList<>();
        if ("system".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            HashMap<Integer, Task> taskHashMap1 = new HashMap<>();
            taskHashMap1.put(TaskData.getTaskById(1).getId(),TaskData.getTaskById(1));
            npcs.add(new NPC(100, "村长", 999, new BaseProperty(0, 0, 0, 0), new SpecialProperty(0, 0, 0, 0), "年迈的村长", "去吧,外面的世界更精彩", taskHashMap1));
            HashMap<Integer, Task> taskHashMap2 = new HashMap<>();
            taskHashMap2.put(TaskData.getTaskById(1000).getId(),TaskData.getTaskById(1000));
            npcs.add(new NPC(101, "王大爷", 10, new BaseProperty(0, 0, 0, 0), new SpecialProperty(0, 0, 0, 0), "邻居王大爷", "喝茶吗？", taskHashMap2));
        }
        if ("mysql".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            try {
                ResultSet rs = JDBCUtil.executeQuery("select * from npc");
                while (rs.next()) {
                    NPC npc = new NPC();
                    npc.setId(rs.getInt("id"));
                    npc.setName(rs.getString("name"));
                    npc.setLevel(rs.getInt("level"));
                    npc.setSpeakText(rs.getString("speak_text"));
                    npc.setDescription(rs.getString("description"));
                    npc.setBaseProperty((BaseProperty) PropertyData.getBasePropertyById(rs.getInt("base_property")));
                    npc.setSpecialProperty((SpecialProperty) PropertyData.getSpecialPropertyById(rs.getInt("special_property")));
                    //放入任务
                    if (rs.getString("task_map") == null || "".equalsIgnoreCase(rs.getString("task_map"))) {
                        npc.setTaskMap(null);
                    } else {
                        String[] task_maps = rs.getString("task_map").split("-");
                        HashMap<Integer, Task> taskHashMap = new HashMap<>();
                        for (String id : task_maps) {
                            Task task = TaskData.getTaskById(Integer.parseInt(id));
                            taskHashMap.put(task.getId(), task);
                        }
                        npc.setTaskMap(taskHashMap);
                    }
                    npcs.add(npc);
                }
                JDBCUtil.close(rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return npcs;
    }

    public static Map<Integer, NPC> npcInit() {
        HashMap<Integer, NPC> npcMap = new HashMap<>();
        for (Role npc : roleList) {
            if (npc instanceof NPC){
                npcMap.put(npc.getId(),(NPC)npc);
            }
        }
        return npcMap;
    }
    public static List<Role>roleInit(){
        ArrayList<Role> roles = new ArrayList<>();
        roles.addAll(getNpcs());
        roles.addAll(getMonsters());
        return roles;
    }
}
